from PIL import Image
from xml.dom.minidom import parse 
def blue(old,new):
    img=Image.open(old)
    pixels = img.load()
    x,y=img.size
    for i in range(x):
        for j in range(y):
            (r,g,b)=pixels[i,j]
            pixels[i,j] = (0, 0, b)
    img.save(new,"PNG")

def hui_du(old,new):
    img=Image.open(old)
    pixels=img.load()
    x,y=img.size
    for i in range(x):
        for j in range(y):
            (r,g,b)=pixels[i,j]
            avg=int((r+g+b)/3)
            pixels[i,j]=(avg,avg,avg)
    img.save(new,"PNG")
    
def red(old,new):
    img=Image.open(old)
    pixels=img.load()
    x,y=img.size
    for i in range(x):
        for j in range(y):
            (r,g,b)=pixels[i,j]
            pixels[i,j] = (r, 0, 0)
    img.save(new,"PNG")
    
def green(old,new):
    img=Image.open(old)
    pixels=img.load()
    x,y=img.size
    for i in range(x):
        for j in range(y):
            (r,g,b)=pixels[i,j]
            pixels[i,j] = (0,g,0)
    img.save(new,"PNG")

def bl(old,new):
    img=Image.open(old)
    pixels=img.load()
    x,y=img.size
    for i in range(x):
        for j in range(y):
            (r,g,b)=pixels[i,j]
            new_r=int(r*1.1)
            new_g=int(g*1.1)
            new_b=int(b*1.1)
            pixels[i,j]=(new_r,new_g,new_b)
    img.save(new,"PNG")


def ban(old,new):
    img=Image.open(old)
    pixels=img.load()
    x,y=img.size
    for i in range(x):
        for j in range(y):
            (r,g,b)=pixels[i,j]
            new_r=int(r/1.1)
            new_g=int(g/1.1)
            new_b=int(b/1.1)
            pixels[i,j]=(new_r,new_g,new_b)
    img.save(new,"PNG")

def b_hui(p,old,new):
    img=Image.open(old)
    pixels=img.load()
    x,y=img.size
    for i in range(x):
        for j in range(y):
            (r,g,b)=pixels[i,j]
            avg=int((r+b+g)/3)
            if (r>avg*float(p)):
                pixels[i,j]=(avg,avg,avg)
    img.save(new,"PNG")


def lp(p,old1,old2,new):
    img=Image.open(old1)
    back=Image.open(old2)
    pixels1=img.load()
    pixels2=back.load()
    x,y=img.size
    for i in range(x):
        for j in range(y):
            (r,g,b)=pixels1[i,j]
            avg=int((r+g+b)/3)
            if (r>avg * float(p)):
                (r1,g1,b1) = pixels2[i,j]
                pixels1[i,j] = (r1,g1,b1)
    img.save(new,"PNG")

def getData(e,p):
    ls = e.getElementsByTagName(p)[0]
    m = ls.childNodes[0].data
    return m
            
def chuli(s):
    doc=parse(s)
    l=doc.getElementsByTagName("image")
    for x in l:
        k = x.getAttribute("name")
        if(k=="蓝通图"):
            ip=getData(x,"old")
            op=getData(x,"new")
            blue(ip,op)
        elif(k=="灰度图"):
            ip=getData(x,"old")
            op=getData(x,"new")
            hui_du(ip,op)
        elif(k=="红通图"):
            ip=getData(x,"old")
            op=getData(x,"new")
            red(ip,op)
        elif(k=="绿通图"):
            ip=getData(x,"old")
            op=getData(x,"new")
            green(ip,op)
        elif(k=="变亮10%"):
            ip=getData(x,"old")
            op=getData(x,"new")
            bl(ip,op)
        elif(k=="变暗10%"):
            ip=getData(x,"old")
            op=getData(x,"new")
            ban(ip,op)
        elif(k=="部分灰度"):
            num=getData(x,"beishu")
            ip=getData(x,"old")
            op=getData(x,"new")
            b_hui(num,ip,op)
        elif(k=="蓝屏特效"):
            num=getData(x,"beishu")
            ip=getData(x,"old")
            ip1=getData(x,"in")
            op=getData(x,"new")
            lp(num,ip,ip)